package edu.jmu.aspectj.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 切面类，再次编写通知
 * @author zheng
 *
 */
@Aspect
@Component
public class MyAspect {
	/**
	 * 用于取代：<aop:pointcut * expression = "execution(* edu.jmu.dao..*.* (..))" id="myPointCut" />
	 * 要求：方法必须是private，没有值、 名称自定义，没有参数
	 */
	@Pointcut("execution(* edu.jmu.dao..*.* (..))")
	private void myPointCut(){};
	
	//前置通知
	@Before("myPointCut()")
	public void myBefore(JoinPoint joinPoint){
		System.out.println("前置通知，目标：");
		System.out.print(joinPoint.getTarget()+",方法名称：");
		System.out.println(joinPoint.getSignature().getName());
	}
	
	@AfterReturning("myPointCut()")
	public void myAfterReturning(JoinPoint joinPoint){
		System.out.println("后置通知:"+joinPoint.getSignature().getName());
	}
	
	@Around("myPointCut()")
	public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		System.out.println("环绕开始");
		Object object = proceedingJoinPoint.proceed();
		System.out.println("环绕结束");
		return object;
	}
	//异常通知
	@AfterThrowing(value="myPointCut()", throwing="e")
	public void myAfterTrhowing(JoinPoint joinPoint, Throwable e){
		System.out.println("异常通知：出错了!"+e.getMessage());
	}
	
	//最终通知
	@After("myPointCut()")
	public void myAfter(){
		System.out.println("最终通知");
	}
}
